package com.hlab.fabrevealmenu.helper;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

/**
 * AnimationHelper
 *
 * @since 2021-05-14
 */
public class AnimationHelper {
    private static final int INT_500 = 500;
    private static final float FLOAT_005 = 0.1f;
    private final int duration;
    private AnimatorProperty startAnimatorValue;
    private AnimatorProperty endAnimatorValue;
    private boolean isShowMenuAni = false;

    /**
     * AnimationHelper
     *
     * @param duration duration
     */
    public AnimationHelper(int duration) {
        this.duration = duration;
    }

    /**
     * revealMenu
     *
     * @param baseView baseView
     * @param startView startView
     * @param endView endView
     * @param isReturning isReturning
     */
    public void revealMenu(ComponentContainer baseView,
                           ComponentContainer startView, ComponentContainer endView, boolean isReturning) {
        Component start = isReturning ? endView : startView;
        Component end = isReturning ? startView : endView;
        end.setScale(0,0);
        end.setVisibility(Component.VISIBLE);
        startAnimatorValue = start.createAnimatorProperty();
        endAnimatorValue = end.createAnimatorProperty();
        startAnimatorValue.setDuration(duration);
        startAnimatorValue.scaleX(0).scaleY(0);
        startAnimatorValue.start();
        endAnimatorValue.setDuration(duration);
        endAnimatorValue.scaleX(1).scaleY(1);
        endAnimatorValue.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                isShowMenuAni = true;
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                isShowMenuAni = false;
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        endAnimatorValue.start();
    }

    /**
     * showOverlay
     *
     * @param overlayLayout overlayLayout
     */
    public void showOverlay(Component overlayLayout) {
        AnimatorValue animatorValue = new AnimatorValue();
        animatorValue.setDuration(duration);
        animatorValue.setValueUpdateListener((animatorValue1, v) -> {
            overlayLayout.setTag(animatorValue);
            overlayLayout.setAlpha(v);
            if (v >= FLOAT_005) {
                overlayLayout.setVisibility(Component.VISIBLE);
            }
        });
        animatorValue.start();
    }

    /**
     * hideOverlay
     *
     * @param overlayLayout overlayLayout
     */
    public void hideOverlay(Component overlayLayout) {
        AnimatorValue animatorValue = (AnimatorValue)overlayLayout.getTag();
        if (animatorValue != null) {
            animatorValue.cancel();
        }
        AnimatorProperty animatorProperty = overlayLayout.createAnimatorProperty();
        animatorProperty.alpha(0).setDuration(duration).setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                overlayLayout.setAlpha(0);
                overlayLayout.setVisibility(Component.INVISIBLE);
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        animatorProperty.start();
    }

    public boolean isShowMenuAni() {
        return isShowMenuAni;
    }
}
